package supercafe.core;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class DataGetter extends AsyncTask<String, String ,Void> {
    private String responsedResult="";
    private Activity ac;
    private boolean finish = false;
    public DataGetter(Activity ac){
    	this.ac = ac;
    	responsedResult="";
    }
    @Override
    protected Void doInBackground(String... params) {
          InputStream content = null;
          String url = params[0];
          try {
            HttpClient httpclient =new DefaultHttpClient();
            HttpResponse response = httpclient.execute(new HttpGet(url));
            content = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String temp;
            while((temp = reader.readLine())!=null){
            	responsedResult+=temp;
            }
          } catch (Exception e) {
                Log.e("Network exception","@nhan-DataGetter" );
                e.printStackTrace();
          }
          return(null);
    }
    @Override
    protected void onProgressUpdate(String... values) {
    }
    @Override
    protected void onPostExecute(Void result) {
        //TextView btn=(TextView)findViewById(R.id.button);
        //btn.setText(ss);
    	finish = true;
    	Toast.makeText(ac, "Lấy dữ liệu xong", Toast.LENGTH_LONG).show();
    }
    public boolean isFinished(){
    	return finish;
    }
    public String getResponseResult(){
    	return this.responsedResult;
    }
}
